<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class FlinkController extends Controller
{
    
     // 链接列表页
    public function getIndex(Request $request){
         if(empty(session('id'))){
        return redirect('/admin')->with('error','请您登陆');
      }
        // 分页
        $count = $request -> input('count',10);
       // 关键字
        $search = $request -> input('search','');
        // 对查询语句构造器进行分页  paginate  查询数据并且分页
        $data = DB::table('flink') ->where('webname','like','%'.$search.'%')-> paginate($count);
        return view('admin/flink/index ',['data'=>$data,'count'=>$count,'search'=>$search,'request'=>$request->all()]);
    }

    //链接添加
    public function getAdd(){
        return view('admin/flink/add');
    }
    //接受链接添加form表单传的值 插入到数据库中
    public function postInsert(Request $request){
        //dd($request->all());
          // 数据组装
         $arr = $request -> except('_token');
        
        // 网站logo
       if($request -> hasFile('logo')){
            // 随机文件名
            $temp_name = md5(time().rand(100000,999999));
            // 获取文件后缀名
            $hz = $request -> file('logo') -> getClientOriginalExtension();
            // 拼接文件名
            $name = $temp_name.'.'.$hz;
            $request -> file('logo') -> move('./upload/flink',$name);
        }
        
        $arr['logo'] = $name;  
        //dd($arr);
        
       //进行添加
       $res = DB::table('flink') -> insert($arr);
    
       if($res){
            return redirect('/admin/flink/index') -> with('success','添加成功');
       }else{
            return back() -> with('error','添加失败');
       } 
    }

    // 用户删除
    public function getDelete($id){
       $data =  DB::table('flink') -> where('id',$id)-> delete();
       //dd($data);
        if($data = 1){

            return redirect('/admin/flink/index') -> with('success','删除成功');
       }else{
            return back() -> with('error','删除失败');
       } 
    }
    // 用户修改页
    public function getEdit($id){
    //查询单条数据
    $data = DB::table('flink') ->where('id',$id) -> first(); 
  
    return view('admin/flink/edit',['data'=>$data]);
    }
    // 用户修改
    public function postUpdate(Request $request){
       //dd($request->all());
        $arr = $request -> except(['_token','id']);
    
        // 获取id时
        $id = $request -> input('id');
          if($request -> hasFile('logo')){
            // 随机文件名
            $temp_name = md5(time().rand(100000,999999));
            // 获取文件后缀名
            $hz = $request -> file('logo') -> getClientOriginalExtension();
            // 拼接文件名
            $name = $temp_name.'.'.$hz;
            $request -> file('logo') -> move('./upload/flink',$name);
             $arr['logo'] = $name;  
        }
        
       
        
        $data = DB::table('flink') ->where('id',$id)-> update($arr);
          if($data = 1){
            
            return redirect('/admin/flink/index') -> with('success','修改成功');
       }else{
            return back() -> with('error','修改失败');
       } 
    }
}
